// pages/movie-detail/movie-detail.js
import {onChange,changeCastsArray} from "../../util/util.js"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movies:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    const mid = options.mid
    wx.request({
      url: app.gmovie + 'subject/' + mid,
      success:(res)=>{
        this.setData({
          movies:res.data
        })
        this.onReadyMovie(this.data.movies)
        console.log(res.data)
      }
    })
  },

  //放大图片
  /**放大图片 */
  onViewPost(event){
    wx.previewImage({
      urls: [this.data.movies.img],
    })
  },

  //对数据进行预处理
  onReadyMovie(movies){
    // console.log(movies)
    const data = {}
    data.title = movies.title
    data.img = movies.images.large
    data.countries = movies.countries[0] + '.' + movies.year
    data.wish_count = movies.wish_count
    data.comments_count = movies.comments_count
    data.rating = movies.rating.stars/10
    data.average = movies.rating.average
    data.directors = onChange(movies.directors)
    data.casts = onChange(movies.casts)
    data.genres = movies.genres.join(", ")
    data.summary = movies.summary
    data.cast = changeCastsArray(movies.casts)
    console.log(data.cast)
    this.setData({
      movies:data
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})